<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="25%" :title="`${drawerProps.title}-延迟队列消息`">
    <el-form ref="ruleFormRef" label-width="100px" label-suffix=":" :rules="rules" :model="operateDataInfo">
      <!-- 表单内容-->
      <el-form-item label="消息内容" prop="message">
        <el-input type="textarea" v-model="operateDataInfo.message" placeholder="请输入消息内容" clearable></el-input>
      </el-form-item>
      <el-form-item label="预计时间" prop="expectTime">
        <el-date-picker
          v-model="operateDataInfo.expectTime"
          type="datetime"
          placeholder="请选择预计时间"
          :value-format="DATE_TIME_FORMAT"
          :shortcuts="shortcuts"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import {
  DelayQueueMessagePageResult,
  DelayQueueMessageSaveParam,
  DelayQueueMessageUpdateParam,
  getDelayQueueMessageInfo
} from "@/api/modules/queue/DelayQueueMessage";
import { DATE_TIME_FORMAT } from "@/utils";

const shortcuts = [
  {
    text: "五分钟后",
    value: () => {
      const date = new Date();
      date.setMinutes(date.getMinutes() + 5);
      return date;
    }
  },
  {
    text: "十分钟后",
    value: () => {
      const date = new Date();
      date.setMinutes(date.getMinutes() + 10);
      return date;
    }
  },
  {
    text: "三十分钟后",
    value: () => {
      const date = new Date();
      date.setMinutes(date.getMinutes() + 30);
      return date;
    }
  },
  {
    text: "一小时后",
    value: () => {
      const date = new Date();
      date.setHours(date.getHours() + 1);
      return date;
    }
  },
  {
    text: "两小时后",
    value: () => {
      const date = new Date();
      date.setHours(date.getHours() + 2);
      return date;
    }
  },
  {
    text: "一天后",
    value: () => {
      const date = new Date();
      date.setDate(date.getDate() + 1);
      return date;
    }
  }
];

interface DrawerProps {
  title: string;
  isSave: boolean;
  isUpdate: boolean;
  row: Partial<DelayQueueMessagePageResult>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
  enumMap?: Map<string, { [key: string]: any }[]>;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  title: "",
  isSave: false,
  isUpdate: false,
  row: {}
});

const rules = reactive({
  // 表单规则, 例如: userName: [{ required: true, message: "请输入用户名" }]
  message: [{ required: true, message: "请输入消息内容" }],
  expectTime: [{ required: true, message: "请选择预计时间" }]
});

const operateDataInfo = ref<DelayQueueMessageSaveParam | DelayQueueMessageUpdateParam>({});
const openDrawer = async (params: DrawerProps) => {
  if (params.isUpdate) {
    // 更新信息，先查询
    let { data } = await getDelayQueueMessageInfo(params.row.id ? params.row.id : "");
    operateDataInfo.value = { ...data };
  }

  if (params.isSave) {
    // 新增前的默认值
    operateDataInfo.value = { queueId: params.row.queueId };
  }
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(operateDataInfo.value);
      ElMessage.success({ message: `${drawerProps.value.title} 成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  openDrawer
});
</script>

<style scoped lang="scss"></style>
